/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.l2emu.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
import java.util.logging.Logger;

/**
 * @author Rayan
 * @since 3795
 * @project L2Emu Project
 */

public class Version
{
	private static Logger _log = Logger.getLogger(Version.class.getName());

	private String				_revisionNumber		= null;
	private String				_builtBy			= null;
	private String				_createdBy			= null;
	private String				_manifestVersion	= null;
	private String				_archiverVersion	= null;
	private String				_buildJdk			= null;
	private String				_coreVersion		= null;
	
	/**
	 * Stable Release
	 */
	public static String		STABLE_RELEASE 		= "1.0.2";

	/**
	 * @param jarName
	 */
	public void loadInformation(String jarName)
	{
		try
		{
			JarFile jarFile = new JarFile("./../libs/" + jarName);

			Attributes attrs = jarFile.getManifest().getMainAttributes();

			//gets info
			setManifestVersion(attrs);
			setArchiverVersion(attrs);
			setCreatedBy(attrs);
			setBuiltBy(attrs);
			setBuildJdk(attrs);
			setImplementationBuild(attrs);
		}
		catch (IOException e)
		{
			_log.warning("Unable to get Soft information\nFile name '" + (jarName == null ? "null" : jarName) + "' isn't a valid jar");
		}
	}

	public void loadCoreInformation() throws IOException
	{
		String path = "./../libs/";
		File dir = new File(path);
		File fList[] = dir.listFiles();

		for (int i = 0; i < fList.length; i++)
		{
			//we set core version here
			if (fList[i].getName().startsWith("l2emu-game"))
				_coreVersion = fList[i].getName().substring(11);
		}
	}

	/**
	 * @param jarName
	 * @param type
	 * @param fileToWrite
	 */
	public void transferInfo(String jarName, String type, File fileToWrite)
	{
		try
		{
			if (!fileToWrite.exists())
			{
				_log.warning("Unable to Find File :" + fileToWrite.getName() + " Please Update your " + type);
				return;
			}

			// Open the JAR file
			JarFile jarFile = new JarFile("./../libs/" + jarName);

			// Get the manifest
			Manifest manifest = jarFile.getManifest();

			// Write the manifest to a file
			OutputStream fos = new FileOutputStream(fileToWrite);
			manifest.write(fos);
			fos.close();
		}
		catch (IOException e)
		{
			_log.warning("L2Emu-Game: Error, " + e);
		}
	}

	/**
	 * @param attrs
	 */
	private void setArchiverVersion(Attributes attrs)
	{
		String archiverVersion = attrs.getValue("Archiver-Version");

		if (archiverVersion != null)
			_archiverVersion = archiverVersion;
		else
			_archiverVersion = "unknown";
	}

	/**
	 * @param attrs
	 */
	private void setManifestVersion(Attributes attrs)
	{
		String manifestVersion = attrs.getValue("Manifest-Version");

		if (manifestVersion != null)
			_manifestVersion = manifestVersion;
		else
			_manifestVersion = "unknown";
	}

	/**
	 * @param attrs
	 */
	private void setImplementationBuild(Attributes attrs)
	{
		String revisionNumber = attrs.getValue("Implementation-Build");

		if (revisionNumber != null)
			_revisionNumber = revisionNumber;
		else
			_revisionNumber = "unknown";
	}

	/**
	 * @param attrs
	 */
	private void setBuildJdk(Attributes attrs)
	{
		String buildJdk = attrs.getValue("Build-Jdk");

		if (buildJdk != null)
			_buildJdk = buildJdk;
		else
			_buildJdk = "unknown";
	}

	/**
	 * @param attrs
	 */
	private void setCreatedBy(Attributes attrs)
	{
		String createdBy = attrs.getValue("Created-By");

		if (createdBy != null)
			_createdBy = createdBy;
		else
			_createdBy = "unknown";
	}

	/**
	 * @param attrs
	 */
	private void setBuiltBy(Attributes attrs)
	{
		String builtBy = attrs.getValue("Built-By");

		if (builtBy != null)
			_builtBy = builtBy;
		else
			_builtBy = "unknown";
	}

	public String getRevisionNumber()
	{
		return _revisionNumber;
	}

	public String getManifestVersion()
	{
		return _manifestVersion;
	}

	public String getCreatedBy()
	{
		return _createdBy;
	}

	public String getArchiverVersion()
	{
		return _archiverVersion;
	}

	public String getBuiltBy()
	{
		return _builtBy;
	}

	public String getBuildJdk()
	{
		return _buildJdk;
	}

	public String getCoreVersion()
	{
		return _coreVersion;
	}
}